package com.xuanwei.core.util;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/4/15 15:01
 **/
public class DateUtil {

    private static final String DATETIME = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE = "yyyy-MM-dd";
    private static final DateTimeFormatter dateTimeFormatter;
    private static final DateTimeFormatter dateFormatter;

    static {
        dateTimeFormatter = DateTimeFormatter.ofPattern(DATETIME);
        dateFormatter = DateTimeFormatter.ofPattern(DATE);
    }

    public static LocalDateTime parseDateTime(String dateTime) {
        return LocalDateTime.parse(dateTime, dateTimeFormatter);
    }

    public static String formatDateTime(LocalDateTime localDateTime) {
        return localDateTime.format(dateTimeFormatter);
    }

    public static LocalDate parseDate(String date) {
        return LocalDate.parse(date, dateFormatter);
    }

    public static String formatDate(LocalDate localDate) {
        return localDate.format(dateFormatter);
    }

    public static Date LocalDateTimeToDate(LocalDateTime localDateTime){
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }

}
